<template>
  <div>
    <p>
      <button class="btn btn-white btn-default btn-round" @click="add()">
        <i class="ace-icon fa fa-edit"></i>
        新增
      </button>
      &nbsp;
      <button class="btn btn-white btn-default btn-round" @click="list(1)">
        <i class="ace-icon fa fa-refresh"></i>
        刷新
      </button>
    </p>

    <pagination ref="pagination" :list="list"></pagination>

    <table id="simple-table" class="table  table-bordered table-hover">
      <thead>
        <tr>
                    <th>id</th>
          <th>手机号</th>
          <th>验证码</th>
          <th>用途</th>
          <th>生成时间</th>
          <th>状态</th>
          <th>操作</th>
        </tr>
      </thead>

      <tbody>
        <tr v-for="sms in smss"
          :key="sms.id"
          :index="sms.id">
          <td>{{sms.id}}</td>
          <td>{{sms.mobile}}</td>
          <td>{{sms.code}}</td>
          <td>{{SMS_USE | optionKV(sms.use)}}</td>
          <td>{{sms.at}}</td>
          <td>{{SMS_STATUS | optionKV(sms.status)}}</td>

          <td>
            <div class="btn-group">
              <button class="btn btn-xs btn-info" @click="edit(sms)">
                <i class="ace-icon fa fa-pencil bigger-120"></i>
              </button>
              <button class="btn btn-xs btn-danger" @click="del(sms.id)">
                <i class="ace-icon fa fa-trash-o bigger-120"></i>
              </button>
            </div>
          </td>
        </tr>
      </tbody>
    </table>

    <!-- Modal -->
    <div id="form-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
              aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">短信验证码表单</h4>
          </div>
          <div class="modal-body">
            <form class="form-horizontal">
              <div class="form-group">
                <label class="col-sm-2 control-label">手机号</label>
                <div class="col-sm-10">
                    <input class="form-control" v-model="sms.mobile">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">验证码</label>
                <div class="col-sm-10">
                    <input class="form-control" v-model="sms.code">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">用途</label>
                <div class="col-sm-10">
                  <select v-model="sms.use" class="form-control">
                    <option v-for="o in SMS_USE" :value="o.key">{{o.value}}</option>
                  </select>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">生成时间</label>
                <div class="col-sm-10">
                    <input class="form-control" v-model="sms.at">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">状态</label>
                <div class="col-sm-10">
                  <select v-model="sms.status" class="form-control">
                    <option v-for="o in SMS_STATUS" :value="o.key">{{o.value}}</option>
                  </select>
                </div>
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            <button type="button" class="btn btn-primary" @click="save()">保存</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import Pagination from "../../components/pagination"

  export default {
    name: "business-sms",
    components: {Pagination},
    data() {
      return {
        sms: {
          id: null,
          mobile: null,
          code: null,
          use: null,
          at: null,
          status: null,
        },
        smss: [],
        SMS_USE: SMS_USE,
        SMS_STATUS: SMS_STATUS,
      }
    },
    mounted() {
      let _this = this
      _this.list(1)
    },
    methods: {
      /**
       * 获取短信验证码（分页）
       * @param page
       */
      list(page) {
        let _this = this

        Loading.show()

        _this.$axios.post(process.env.VUE_APP_SERVER + '/business/admin/sms/list', {
          page: page,
          size: _this.$refs.pagination.size
        }).then((resp) => {
          if (resp.data.success) {
            _this.smss = resp.data.content.list
            _this.$refs.pagination.render(page, resp.data.content.total)

            Loading.hide()
          } else {
            Loading.hide()
          }
        })
      },

      /**
       * 添加短信验证码（显示模态框）
       */
      add() {
        let _this = this
        _this.sms = {
          id: null,
          mobile: null,
          code: null,
          use: null,
          at: null,
          status: null,
        }
        //  开启模态框
        $("#form-modal").modal('show')
      },

      /**
       * 编辑短信验证码（显示模态框）
       */
      edit(sms) {
        let _this = this
        //  Object.assign(target, source)方法用于对象的合并，将源对象（source）的所有可枚举属性，复制到目标对象（target)
        Object.assign(_this.sms, sms)
        //  开启模态框
        $("#form-modal").modal('show')
      },

      /**
       * 保存短信验证码
       */
      save() {
        let _this = this

        //  保存校验
        if (1 != 1
          || !Validator.require(_this.sms.mobile, "手机号")
          || !Validator.length(_this.sms.mobile, "手机号", 1, 50)
          || !Validator.require(_this.sms.code, "验证码")
          || !Validator.require(_this.sms.use, "用途")
          || !Validator.require(_this.sms.at, "生成时间")
          || !Validator.require(_this.sms.status, "状态")
        ) {
          return;
        }

        _this.$axios.post(process.env.VUE_APP_SERVER + '/business/admin/sms/save', _this.sms).then((resp) => {
          if (resp.data.success) {
            //  关闭模态框
            $("#form-modal").modal('hide')
            //  刷新表格
            _this.list(1)
            Toast.success('保存成功！')
          } else {
            Toast.warning(resp.data.message)
          }
        })
      },

      /**
       * 删除短信验证码（根据id）
       * @param id
       */
      del(id) {
        let _this = this
        Confirm.show('删除短信验证码后不可恢复，是否确认删除？', () => {
          _this.$axios.delete(process.env.VUE_APP_SERVER + '/business/admin/sms/delete/' + id).then((resp) => {
            if (resp.data.success) {
              //  刷新表格
              _this.list(1)
              Toast.success('删除成功！')
            }
          })
        })
      }
    }
  }
</script>

<style scoped>

</style>